//
//  AppDelegate.swift
//  RSWeibo
//
//  Created by Macx on 16/8/8.
//  Copyright © 2016年 Macx. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // 注册通知监听者
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootVC:", name: changeRootViewController, object: nil)
        
        
        // 1,创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        // 2,创建控制器
        let rootVC = RSMainTabBarController()

        
        // 3,设置根控制器
        window?.rootViewController = rootVC
        
        // 4,设置window为主窗口并可见
        window?.makeKeyAndVisible()
        
        
        return true
    }
    
    // 完成监听方法
    @objc private func changeRootVC(noti: NSNotification){
        
        // 判断是哪个控制器发来的通知, 根据不同控制器的通知确定跳转到不同的根控制器
        if noti.object is RSOauthViewController {
            // 如果是RSOauthViewController发来的通知, 说明登录成功了, 跳转到欢迎控制器
            window?.rootViewController = RSWelcomeViewController()
        }else{
        
            // 如果是访客界面发送过来的通知, 则说明已经是处在登录状态, 就跳转到主界面
            window?.rootViewController = RSMainTabBarController()
        }
    
//        print("什么鬼")
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

